const fs = require("fs");
const path = require("path");
const rootRouter = require('express').Router()

let viewsPath = path.join(__dirname, '../../views/')
let prefixPath = `pages/`

function pagesRegister() {

    let adminDirs = fs.readdirSync(viewsPath + prefixPath + '/admin', {encoding: 'utf8'})

    for (const dir of adminDirs) {
        if (dir.endsWith('.page.html')) {
            let path;
            path = dir.replace('.page.html', '');
            rootRouter.get(`/admin/` + path, (req, res) => {
                let {query, params} = req
                console.log(query, params)
                res.render(prefixPath + '/admin/' + dir, {query, params})
            })
        }

    }

    rootRouter.get('/', (req, res) => {
        res.render('index.html', {name: 'gqq'}, (err, html) => {
            if (err) return res.send(err);
            res.send(html)
        });
        // use data you need code {{ variable }} stu link to https://juejin.cn/post/7195018707435782201

    })


    rootRouter.get('/admin', (req, res) => {
        res.redirect(301,'/admin/index')
    })

    rootRouter.get('/admin/demo/:type', (req, res) => {
        let type = req.params.type || 'index'
        console.log(type)
        res.render(`components/demo/${type}.html`)
    })
    return rootRouter
}


module.exports = pagesRegister()
